package com.rstrulak.managedBeans.controllers;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.ContextDao;
import com.rstrulak.dao.TaskAttachmentsDao;
import com.rstrulak.dao.TaskDao;
import com.rstrulak.dao.UserDao;
import com.rstrulak.model.Context;
import com.rstrulak.model.Task;
import com.rstrulak.model.TaskAttachments;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;
import com.rstrulak.utils.NoteFile;

@Component("editTaskBean")
@Scope("request")
public class EditTaskBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -945358760825549817L;

	@Autowired
	UserDao userDao;

	@Autowired
	TaskDao taskDao;

	@Autowired
	ContextDao contextDao;

	@Autowired
	private TaskAttachmentsDao taskAttachmentsDao;

	private Task task;

	private String taskName;

	private String taskDescription;

	private String contextName;

	private String ownerName;

	private Map<String, Long> allContextMap;

	private Long taskId;

	private List<TaskAttachments> taskAttachmentsList;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
		
		getTask();

	}

	public Task getTask() {
		taskId = (Long) JSFUtils.flashScope().get(Constants.TASK_ID);
		JSFUtils.flashScope().put(Constants.TASK_ID, taskId);

		if (taskId == null) {
			JSFUtils.setFlashFromSession();
			taskId = (Long) JSFUtils.flashScope().get(Constants.TASK_ID);
			JSFUtils.flashScope().put(Constants.TASK_ID, taskId);
		}

		getTaskFromDb(taskId);

		intializeField();

		initializeContext();

		if (task != null) {
			taskAttachmentsList = taskAttachmentsDao.getTaskAttachemntsListByTask(task);
		}

		return task;
	}

	private void initializeContext() {
		allContextMap = new HashMap<String, Long>();
		List<Context> contextList = contextDao.getAll();
		for (Context context : contextList) {
			allContextMap.put(context.getName(), context.getId());
		}
	}

	private void intializeField() {
		taskName = task.getTaskName();
		taskDescription = task.getTaskDescription();

		if (task.getContext() != null) {
			contextName = "" + task.getContext().getId();
		}
	}

	private void getTaskFromDb(Long taskId) {
		task = taskDao.getById(taskId);
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public String getTaskDescription() {
		return taskDescription;
	}

	public void setTaskDescription(String taskDescription) {
		this.taskDescription = taskDescription;
	}

	public Map<String, Long> getAllContextMap() {
		return allContextMap;
	}

	public void setAllContextMap(Map<String, Long> allContextMap) {
		this.allContextMap = allContextMap;
	}

	public String getContextName() {
		return contextName;
	}

	public void setContextName(String contextName) {
		this.contextName = contextName;
	}

	public String getOwnerName() {
		return ownerName;
	}

	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	public List<TaskAttachments> getTaskAttachmentsList() {
		return taskAttachmentsList;
	}

	public void setTaskAttachmentsList(List<TaskAttachments> taskAttachmentsList) {
		this.taskAttachmentsList = taskAttachmentsList;
	}

	public void updateTask(FileUploadBean fileUploadBean) throws ServletException, IOException {
		task.setTaskName(taskName);
		task.setTaskDescription(taskDescription);
		if (contextName != null) {
			task.setContext(contextDao.getById(new Long(contextName)));
		}
		taskDao.saveOrUpdate(task);

		FileUploadBean uploadBean = fileUploadBean;
		ArrayList<NoteFile> filesArray = uploadBean.getFiles();
		List<TaskAttachments> taskAttachmentsList = new ArrayList<TaskAttachments>();

		for (int i = 0; i < filesArray.size(); i++) {
			TaskAttachments taskAttachments = new TaskAttachments();
			String oldName = filesArray.get(i).getName();
			StringBuilder finalName = new StringBuilder();
			finalName.append("taskId_");
			finalName.append(task.getId());
			finalName.append("_fileName_");
			finalName.append(oldName);

			taskAttachments.setTask(task);
			taskAttachments.setFilePath(Constants.TASK_ATTACHMENTS_PATH + finalName);
			taskAttachments.setFileName(oldName);
			taskAttachmentsList.add(taskAttachments);

			filesArray.get(i).setName(finalName.toString());
		}

		fileUploadBean.saveTaskFiles();
		fileUploadBean.clearUploadData();
		if (taskAttachmentsList.size() > 0) {
			this.saveTaskAttachments(taskAttachmentsList);
		}

		JSFUtils.redirect("/protected/task.xhtml");
	}

	public void cancelUpdateTask() throws ServletException, IOException {
		JSFUtils.redirect("/protected/task.xhtml");
	}

	public void deleteAttachment(TaskAttachments taskAttachments) {
		taskAttachmentsDao.delete(taskAttachments);
		File file = new File(taskAttachments.getFilePath());
		file.delete();
		taskAttachmentsList = taskAttachmentsDao.getTaskAttachemntsListByTask(task);
	}

	public void saveTaskAttachments(List<TaskAttachments> taskAttachmentsList) {
		for (TaskAttachments taskAttachments : taskAttachmentsList) {
			taskAttachmentsDao.save(taskAttachments);
		}
	}

}
